package com.loong.ems.controller;

import com.loong.ems.domain.entity.EmpAttendanceRecord;
import com.loong.ems.domain.entity.Employee;
import com.loong.ems.domain.entity.SysUser;
import com.loong.ems.domain.vo.resp.NameValueResult;
import com.loong.ems.domain.vo.resp.ResponseCode;
import com.loong.ems.domain.vo.resp.Result;
import com.loong.ems.service.EmpAttendanceRecordService;
import com.loong.ems.service.EmpLeaveRecordService;
import com.loong.ems.service.EmployeeService;
import com.loong.ems.service.SysUserService;
import io.micrometer.common.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Pattern;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 龙勇-229970722
 * @date 2025-01-02 11:40
 * @description: 用户管理
 */
@RestController
@RequestMapping("/user")
@Log4j2
@CrossOrigin
@Tag(name = "用户信息管理 API" , description = "用户信息管理 API")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private EmpAttendanceRecordService empAttendanceRecordService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmpLeaveRecordService empLeaveRecordService;

    /**
     * 获取用户列表
     * @return
     */
    @Operation(summary = "获取用户列表")
    @GetMapping ("/list")
    public Result<List<SysUser>> list() {
        return Result.success(sysUserService.list());
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result login(@RequestParam String username,
                        @RequestParam String password) {
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            return Result.error(ResponseCode.USERNAME_OR_PASSWORD_ERROR);
        }
        SysUser user = sysUserService.findUserByName(username);
        if (user == null) {
            return Result.error(ResponseCode.ACCOUNT_NOT_EXISTS);
        }
        if (!passwordEncoder.matches(password, user.getPassword())) {
            return Result.error(ResponseCode.USERNAME_OR_PASSWORD_ERROR);
        }
//        user.setPassword(null);
        return Result.success(user);
    }
    /**
     * 注册
     * @param user
     * @return
     */
    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result register(@RequestBody SysUser user) {
        if (sysUserService.findUserByName(user.getAccount()) != null) {
            return Result.error(ResponseCode.ACCOUNT_EXISTS_ERROR);
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        sysUserService.save(user);
        return Result.success();
    }

    /**
     * 获取打卡记录
     * @param id
     * @return
     */
    @Operation(summary = "获取打卡记录")
    @GetMapping("/getCar/{id}")
    public Result<List<EmpAttendanceRecord>> getCard(@PathVariable(value = "id") Long id){
        return Result.success(empAttendanceRecordService.getCard(id));
    }

    /**
     * 签到签退
     * @param id
     * @return
     */
    @Operation(summary = "签到签退")
    @PostMapping("/clockIn/{id}")
    public Result<String> clockIn(@PathVariable(value = "id") Long id){
        // 调用服务层的打卡方法
        String resultMessage = empAttendanceRecordService.clockIn(id);

        // 根据结果返回不同的 Result
        if ("打卡成功".equals(resultMessage)) {
            return Result.success(resultMessage);
        } else {
            return Result.error(resultMessage);
        }
    }

    /**
     * 获取员工，请假，迟到，早退人数
      * @return
     */
    @Operation(summary = "获取员工，请假，迟到，缺勤人数")
    @GetMapping("/num")
    public Result<List<NameValueResult>> getNum() {
        List<NameValueResult> list = new ArrayList<>();
        // 员工人数
        NameValueResult emp = new NameValueResult();
        NameValueResult leave = new NameValueResult();
        NameValueResult late = new NameValueResult();
        NameValueResult quit = new NameValueResult();
        // 员工人数
        emp.setName("员工人数");
        int empCount = employeeService.count();
        emp.setValue(String.valueOf(empCount));
        list.add(emp);
        // 请假人数
        leave.setName("请假人数");
        int leaveCount = empLeaveRecordService.countLeave();
        leave.setValue(String.valueOf(leaveCount));
        list.add(leave);
        // 迟到人数
        late.setName("迟到人数");
        int lateCount = empAttendanceRecordService.countLate();
        late.setValue(String.valueOf(lateCount));
        list.add(late);
        // 缺勤人数
        quit.setName("缺勤人数");
        int quitCount = empCount - leaveCount;
        if (lateCount > 0)
            quit.setValue(String.valueOf(quitCount));
        else
            quit.setValue("0");
        list.add(quit);
        return Result.success(list);
    }

    /**
     * 获取部长信息
     * @return
     */
    @Operation(summary = "获取部长信息")
    @GetMapping("/getHeadUser")
    public Result<List<SysUser>> getHeadUser() {
        List<SysUser> user = sysUserService.findUserByRole();
        return Result.success(user);
    }



}
